﻿using System;
using System.Collections;
using OrderManage.DAL;
using OrderManage.Model;
using OrderManage.Utility;

namespace OrderManage.BLL
{
    public class UserBLL
    {
        private readonly UserDAL _dal = new UserDAL();

        public UserInfo Login(UserQcInfo qcmodel)
        {
            return _dal.Login(qcmodel);
        }

        public string GetUserList(UserQcInfo qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetUserList(qcmodel);
            return JsonHelper.Encode(total, list);
        }
        public string GetUserInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetUserInfo(id));
        }

        public bool AddUser(string data)
        {
            var model = new UserInfo();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.UserName = row["UserName"].ToString();
                model.Password = row["Password"].ToString();
                model.Role = Convert.ToInt32(row["Role"].ToString());
                model.FullName = row["FullName"].ToString();
                model.Gender = Convert.ToInt32(row["Gender"].ToString());
                model.IDNO = row["IDNO"] != null ? row["IDNO"].ToString() : "";
                model.PhoneNo = row["PhoneNo"] != null ? row["PhoneNo"].ToString() : "";
                model.Email = row["Email"] != null ? row["Email"].ToString() : "";
                model.QQ = row["QQ"] != null ? row["QQ"].ToString() : "";
            }
            return _dal.AddUser(model);
        }


        public bool ModifyUser(string data)
        {
            var model = new UserInfo();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.UserId = Convert.ToInt32(row["UserId"].ToString());
                model.UserName = row["UserName"].ToString();
                model.Role = Convert.ToInt32(row["Role"].ToString());
                model.FullName = row["FullName"].ToString();
                model.Gender = Convert.ToInt32(row["Gender"].ToString());
                model.IDNO = row["IDNO"] != null ? row["IDNO"].ToString() : "";
                model.PhoneNo = row["PhoneNo"] != null ? row["PhoneNo"].ToString() : "";
                model.Email = row["Email"] != null ? row["Email"].ToString() : "";
                model.QQ = row["QQ"] != null ? row["QQ"].ToString() : "";
            }
            return _dal.ModifyUser(model);
        }

        public bool ModifyPwd(string data)
        {
            var model = new UserInfo();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.UserName = row["UserName"].ToString();
                model.Password = row["NewPwd"].ToString();
            }
            return _dal.ModifyPwd(model);
        }

        public bool RemoveUser(string id)
        {
            return _dal.RemoveUser(id);
        }

        public bool UserNameValidation(string userName)
        {
            return _dal.UserNameValidation(userName);
        }

        public bool PasswordValidation(string userName, string password)
        {
            return _dal.PasswordValidation(userName, password);
        }
    }
}
